package cn.sinata.wanglin.adapter;

import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.model.VillageNeighbor;
import cn.sinata.wanglin.utils.ImageUtil;

/**
 *
 * Created by liaoxiang on 16/12/10.
 */

public class VillageNeighborAdapter extends HFRecyclerAdapter<VillageNeighbor> implements Filterable{

    private VillageNeighborFilter mFilter;
    private ArrayList<VillageNeighbor> mUnfilteredData;

    public VillageNeighborAdapter(List<VillageNeighbor> mData) {
        super(mData, R.layout.item_village_neighbor_layout);
    }

    @Override
    public void onBind(int position, VillageNeighbor villageNeighbor, ViewHolder holder) {
        SimpleDraweeView headView = holder.bind(R.id.headView);
        headView.setImageURI(ImageUtil.parseImageUri(villageNeighbor.getHeadUrl()));
        holder.setText(R.id.tv_name,villageNeighbor.getNickName());
        TextView tv_rank = holder.bind(R.id.tv_rank);
        if (villageNeighbor.getIndex()==1) {
            tv_rank.setBackgroundResource(R.mipmap.active_level1);
        } else if (villageNeighbor.getIndex() == 2) {
            tv_rank.setBackgroundResource(R.mipmap.active_level2);
        } else if (villageNeighbor.getIndex() == 3) {
            tv_rank.setBackgroundResource(R.mipmap.active_level3);
        } else {
            tv_rank.setBackgroundResource(R.mipmap.active_level4);
        }
        tv_rank.setText(String.valueOf(villageNeighbor.getIndex()));
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new VillageNeighborFilter();
        }
        return mFilter;
    }


    private class VillageNeighborFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            Utils.systemErr("-performFiltering--->"+mData.size());
            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList<>(mData);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<VillageNeighbor> list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<VillageNeighbor> unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList<VillageNeighbor> newValues = new ArrayList<>(count);

                for (int i = 0; i < count; i++) {
                    VillageNeighbor h = unfilteredValues.get(i);
                    if (h != null) {
                        String word = h.getNickName();
                        if (word.toLowerCase().contains(prefixString)) {
                            newValues.add(h);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            mData.clear();
            //noinspection unchecked
            mData.addAll((List<VillageNeighbor>) results.values);
//            mData = (List<VillageNeighbor>) results.values;
            notifyDataSetChanged();
        }
    }

    public void resetFilter(){
        mUnfilteredData = null;
        mFilter = new VillageNeighborFilter();
    }

}
